package com.zeusee.main.lpr.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.zeusee.main.lpr.R;
import com.zeusee.main.lpr.jni.PlateRecognition;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

public class MainActivity extends Activity {
    private static final int REQUEST_PERMISSION_STORAGE = 4;
    private final String TAG = getClass().toString();
    public Button pictureBtn;
    public Button cameraPlate;
    public ImageView imgv;
    public TextView resbox;
    public TextView runtimebox;
    public SeekBar sb;

    public void copyFilesFromAssets(Context context, String str, String str2) {
        try {
            String[] list = context.getAssets().list(str);
            if (list.length > 0) {
                if (!new File(str2).mkdir()) {
                    Log.d("mkdir", "can't make folder");
                }
                for (String str3 : list) {
                    copyFilesFromAssets(context, str + "/" + str3, str2 + "/" + str3);
                }
                return;
            }
            InputStream open = context.getAssets().open(str);
            FileOutputStream fileOutputStream = new FileOutputStream(new File(str2));
            byte[] bArr = new byte[1024];
            while (true) {
                int read = open.read(bArr);
                if (read != -1) {
                    fileOutputStream.write(bArr, 0, read);
                } else {
                    fileOutputStream.flush();
                    open.close();
                    fileOutputStream.close();
                    return;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void initRecognizer() {
        String str = Environment.getExternalStorageDirectory() + File.separator + "car";
        copyFilesFromAssets(this, "car", str);
        Log.d("modelPath", str);
        PlateRecognition.getInstance().initPlateRecognition(str);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_PERMISSION_STORAGE && grantResults.length > 0) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                init();
            } else {
                Toast.makeText(this, "应用获取权限失败", Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_PERMISSION_STORAGE);
            }else {
                init();
            }
        }else {
            init();
        }
    }

    private void init() {
        this.pictureBtn = (Button) findViewById(R.id.button);
        this.imgv = (ImageView) findViewById(R.id.imageView);
        this.resbox = (TextView) findViewById(R.id.textView);
        this.sb = (SeekBar) findViewById(R.id.seek);
        this.runtimebox = (TextView) findViewById(R.id.textView3);
        this.cameraPlate = (Button) findViewById(R.id.button3);
        initRecognizer();
        this.cameraPlate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, CameraPlateActivity.class));
            }
        });
        this.pictureBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, ImageActivity.class));
            }
        });
    }


    public void onDestroy() {
        PlateRecognition.getInstance().releaseJni();
        super.onDestroy();
    }
}
